//
//  DismissAnimation.m
//  PayViewDemo
//
//  Created by _Finder丶Tiwk on 15/6/11.
//  Copyright (c) 2015年 _Finder丶Tiwk. All rights reserved.
//

#import "DismissAnimation.h"
#import "StyleSheet.h"

@implementation DismissAnimation

+(instancetype)animationByDismissType:(KDismissAnimationType)dismissType{
    DismissAnimation *animation = [[DismissAnimation alloc] init];
    animation.animationType = dismissType;
    return animation;
}

-(NSTimeInterval)animationDuration{
    return (_animationDuration==0)?0.6:_animationDuration;
}


-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return self.animationDuration;
}


-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *origin = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *destination = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGRect initFrame = [transitionContext initialFrameForViewController:origin];
    CGRect finalFrame = CGRectOffset(initFrame,  screenBounds.size.width,0);
    
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:destination.view];
    [containerView sendSubviewToBack:destination.view];
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    switch (self.animationType) {
        case DismissSlideToRight:
        {
            [UIView animateWithDuration:duration animations:^{
                origin.view.frame = finalFrame;
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];
        }
            break;
        case DismissScaleFromBiglToSmal:
        {
            origin.view.backgroundColor = [UIColor clearColor];
            [UIView animateWithDuration:duration animations:^{
                origin.view.transform = CGAffineTransformMakeScale(0.2, 0.2);
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];

        }
            break;
        case DismissEaseOut:
        {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            [UIView animateWithDuration:duration animations:^{
                origin.view.backgroundColor = [UIColor clearColor];
            } completion:^(BOOL finished) {
                
            }];
        }
            break;
            
        case DismissSlideToBottom:
        {
            CGRect bottomFrame = CGRectOffset(initFrame, 0, screenBounds.size.height);
            [UIView animateWithDuration:duration animations:^{
                origin.view.frame = bottomFrame;
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];
        }
            break;
        default:
            break;
    }
}



@end
